package com.yzyz.service.impl;

import java.awt.Color;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.imageio.ImageIO;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.yzyz.bean.scenic.PictureBean;
import com.yzyz.dao.PictureDao;
import com.yzyz.service.PictureService;
import com.yzyz.utils.CommonHandle;
import com.yzyz.utils.Config;
import com.yzyz.utils.error.Error;

/**
 * 
 * @author David Sun
 * @Date   2010-3-21
 *
 */
public class PictureServiceImpl implements PictureService {
	Log logger = LogFactory.getLog(PictureServiceImpl.class);
	private PictureDao pictureDao;
	@Override
	public void setPictureDao(PictureDao pictureDao) {
		this.pictureDao = pictureDao;
	}
	@Transactional(propagation = Propagation.REQUIRED,rollbackFor=Exception.class)
	@Override
	public int delete(int id) {
		if(this.pictureDao.delete(id) != 1){
			return Error.OBJECT_NOT_EXIST;
		}
		return Error.SUCCESS;
	}

	@Override
	public Object find(int id) {
		
		return this.pictureDao.get(id);
	}
	@Transactional(propagation = Propagation.REQUIRED,rollbackFor=Exception.class)
	@Override
	public int add(Object object) {
		PictureBean picture = (PictureBean)object;
		if(picture.getContents().length > 1024 * 1024 * 2) {
			return Error.PICTURE_SIZE_TOO_LARGE;
		}
		if(picture.getContents() == null || picture.getContents().length == 0) {
			return Error.PICTURE_SIZE_IS_ZERO;
		}
		picture.setId(this.pictureDao.save(object));
		return Error.SUCCESS;
	}
	@Transactional(propagation = Propagation.REQUIRED,rollbackFor=Exception.class)
	@Override
	public int update(Object object) {
		this.pictureDao.update(object);
		PictureBean picture = (PictureBean)object;
		picture.setContents(null);
		return Error.SUCCESS;
	}

	@Override
	public byte[] resize(Image image, int width, int height, String formatName) {
		if (image == null) {
			return null;
		}
		BufferedImage bi = null;
		bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		
		bi.getGraphics().drawImage(image, 0, 0, width, height, Color.WHITE, null);
		ByteArrayOutputStream outPutStream = new ByteArrayOutputStream();
		try {
			ImageIO.write(bi, formatName, outPutStream);
			return outPutStream.toByteArray();
		} catch (IOException e) {
			logger.warn(e.getMessage());
		}
		return null;
	}
	
	@Override
	public BufferedImage bytes2Image(byte[] bytes) {
		BufferedImage image = null;
		InputStream is = new ByteArrayInputStream(bytes);
		try {
			image = ImageIO.read(is);
		} catch (IOException e) {
			logger.warn(e.getMessage());
		}
		return image;
	}

	@Override
	public int checkCount(int scenicId) {
		int count = this.pictureDao.getCount(scenicId);
		if(count >= Integer.valueOf(Config.getConfig(Config.PICTURE_COUNT))) {
			logger.warn(CommonHandle.now() + " The count of pictures of scenic " + scenicId + " is more thatn " + Config.getConfig(Config.PICTURE_COUNT));
			return Error.PICUTURE_OVER_COUNT;
		}
		return Error.SUCCESS;
	}

	@Override
	public List<PictureBean> getPicturesWithoutContents(int ownerId) {
		
		return this.pictureDao.getPicturesWithoutContents(ownerId);
	}

	@Override
	public PictureBean getPicture(int pictureId) {
		List<PictureBean> list = this.pictureDao.getPicture(pictureId);
		return list == null? null: list.size()== 0?null:list.get(0);
	}

	@Override
	public PictureBean getPictureByAttName(String name, int scenicId) {
		
		return this.pictureDao.getPictureByAttName(name, scenicId);
	}
	@Override
	public List<?> paginate(int start, int limit) {
		// TODO Auto-generated method stub
		return null;
	}
	



}
